const util = require('../../utils/util.js')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    data_package: [], //红包数据
    info: { 'status': 0, 'button': '已领取' },
    canCommit: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.share();
    let self = this;
    let uri = '';
    let data = {};
    wx.getStorage({
      key: 'key',
      success: (res) => {
        self.setData({
          member_key: res.data
        });
        uri = app.globalData.ApiUrl + '/index.php?act=member_index&op=member_redpackage_show';
        data = {
          key: self.data.member_key,
        };
        self.getPackage(uri, data)
      },
      fail: () => {
        uri = app.globalData.ApiUrl + '/index.php?act=wechat_index&op=member_redpackage_show';
        self.getPackage(uri, data)
      }
    })

  },
  onShow: function () {
    this.onLoad();
  },
  /**
   * 获取红包数据
   */
  getPackage: function (uri, data) {
    let self = this;
    wx.showLoading({
      title: "加载中",
    })
    wx.request({
      url: uri,
      method: "POST",
      data: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          self.setData({
            data_package: res.data.datas.redpackage,
            info: res.data.datas.get_info
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络请求失败',
          icon: none
        })
      }
    })
  },
  /**
   * 领取红包
   */
  gainPackage: function () {
    let self = this;
    if (!self.data.member_key) {
      if (app.globalData.authorized) {
        wx.navigateTo({
          url: "../chooselogin/chooselogin"
        })
      } else {
        wx.navigateTo({
          url: "../login/login"
        })
      }
      return;
    }
    if (self.data.canCommit == false) {
      return;
    }
    self.data.canCommit == false;
    wx.showLoading({
      title: "加载中",
    })

    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_index&op=member_get_new_redpackage',
      method: "POST",
      data: {
        key: self.data.member_key,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        self.data.canCommit = true;
        if (res.data.code == 200) {
          self.data.info.status = 0;
          self.data.info.button = '已领取';
          self.setData({
            info: self.data.info
          });
          wx.showToast({
            title: '领取成功',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        self.data.canCommit = true;
        wx.showToast({
          title: '网络请求失败',
          icon: none
        })
      }
    })
  },
  onShareAppMessage: function () {
    let share_key = this.data.member_key ? this.data.member_key : '';
    let share_path = '';
    if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
      share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
    } else {
      share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
    }
    return {
      title: '新人有礼,分享有奖',
      path: share_path
    }
  }
})